<?php
/**
 * Created by PhpStorm.
 * User: HP_USER
 * Date: 2018/6/11
 * Time: 11:50
 */

namespace app\data;

use think\Db;

class ProxyDate extends Base
{
    /**
     * 获取市区名称和区域代码
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getProxy()
    {
        return Db::table('china_area')
            ->field([
                'id',
                'name',
                'area_level lv'
            ])->order('area_level desc')
            ->select();
    }
    /**
     * 获取区域信息
     * @param $type_lv
     * @param $p_id
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getAreaInfo($type_lv,$p_id=0)
    {
        return Db::table('china_area')
            ->field([
                'id',
                'name'
            ])->where([
                'area_level' => $type_lv,
                'parent_id'=>$p_id
            ])->select();
    }

    /**
     * @param $id
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public function getArea($id)
    {
        return Db::table('china_area')
            ->field([
                'id',
                'name',
                'parent_id'
            ])->where([
                'id'=>$id
            ])->find();
    }

    /**
     * 获取代理地区信息
     * @param $where
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getProxyAreaInfo($where)
    {
        return Db::table('drh_proxy a')
            ->join('china_area b','a.area_id=b.id','left')
            ->field([
                'a.area_id id',
                'b.name',
            ])->where($where)->select();
    }

    public function getProxyMoney($where)
    {
        $res=Db::table('drh_proxy_user_flow')
            ->field([
                'IFNULL(sum(money_expenses),0) money'
            ])->where($where)
            ->find();
        return $res['money'];
    }

    public function showProvince()
    {
        return Db::table('china_area')
            ->field([
                'id',
                'name',
                'parent_id'
            ])->where([
                'area_level' => 1,
            ])->select();
    }

    public function showCity($p_id)
    {
        return Db::table('china_area')
            ->field([
                'id',
                'name',
                'parent_id'
            ])->where([
                'area_level' => 2,
                'parent_id' => $p_id,
            ])->select();
    }

    public function showDistrict($c_id)
    {
        return Db::table('china_area')
            ->field([
                'id',
                'name',
                'parent_id'
            ])->where([
                'area_level' => 3,
                'parent_id' => $c_id,
            ])->select();
    }

    public function editConfig($name, $change_value)
    {
        Db::table('drh_admin_config')
            ->where([
                'name'=>$name
            ])->update([
                'value'=>$change_value
            ]);
        return true;
    }

    public function addFlowConfig($name, $change_value,$time,$value)
    {
        Db::table('drh_admin_flow_config')
            ->insert([
                'name'=>$name,
                'change_value'=>$change_value,
                'value'=>$value,
                'update_time'=>$time,
            ]);
        return true;
    }



}